//
//  CFTutorViewController.m
//  CFfuturestalent
//
//  Created by 子yu 刘 on 2018/4/26.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "CFTutorViewController.h"
#import "CFTutorItme.h"
#import "CFCalendarView.h"

@interface CFTutorViewController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation CFTutorViewController

- (void)viewDidLoad {
    self.title = @"课程表";
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barBtnItemWithTitle:@"日历" target:self action:@selector(calendar)];
}

- (void)calendar{
    [CFCalendarView showCalenderViewOnView:self selectedData:^(NSDate *date) {
        
    }];

}


-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_scrollIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 8;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CFTutorItme *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CFTutorItme" forIndexPath:indexPath];
    return cell;
}

- (void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    //1.根据偏移量判断一下应该显示第几个item
    CGFloat offSetX = targetContentOffset->x;
    CGFloat itemWidth = 200;
    //item的宽度+行间距 = 页码的宽度
    NSInteger pageWidth = itemWidth + 10;
    //根据偏移量计算是第几页
    NSInteger pageNum = (offSetX+pageWidth/2)/pageWidth;
    //2.根据显示的第几个item，从而改变偏移量
    targetContentOffset->x = pageNum*pageWidth;
    NSLog(@"滚动到哪%ld",(long)pageNum);
   // self.currentIndex = pageNum;
}

-(void)dealloc{
    //选中日期置为当前日期;
    [CFCalendarView sharedInstance].seletedIndex = [DateUtil getNowDateIndexDayInWeek:[NSDate date]]-2;
}

@end
